package cn.bdqn.controller;

import cn.bdqn.entity.BugDetail;
import cn.bdqn.mapper.BugDetailMapper;
import cn.bdqn.service.BugDetailService;
import cn.bdqn.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BugDetailController {
    @Resource
    BugDetailService bugDetailService;

    @RequestMapping(value = {"/","index"})
    public String index(Model model,@RequestParam(defaultValue = "0") int pid,@RequestParam(defaultValue = "1") int pageNo){
        Page<BugDetail> page=new Page<>();
        page.setPageNo(pageNo);
        page.setTotalCount((int) bugDetailService.count());
        page.setList(bugDetailService.selectList(pid,(page.getPageNo()-1)*page.getPageSize(),page.getPageSize()));
        model.addAttribute("page",page);
        return "index";
    }

    @RequestMapping("/del")
    @ResponseBody
    public Object del(int id){
        boolean b = bugDetailService.removeById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("msg",b==true?"删除成功":"删除失败");
        return map;
    }

    @RequestMapping("/{path}.html")
    public String path(@PathVariable("path") String path){
        return path;
    }

    @RequestMapping("/add")
    public String add(BugDetail bugDetail,Model model){
        boolean save = bugDetailService.save(bugDetail);
        model.addAttribute("msg",save==true?"新增成功":"新增失败");
        return "forward:/index";
    }
    @RequestMapping("/toUpdate")
    public String toUpdate(int id,Model model){
        BugDetail detail = bugDetailService.select(id);
        model.addAttribute("bugDetail",detail);
        return  "update";
    }
    @RequestMapping("/update")
    public String update(BugDetail bugDetail,Model model){
        boolean b = bugDetailService.updateById(bugDetail);
        model.addAttribute("msg",b?"修改成功":"修改失败");
        return "forward:/index";
    }
}
